<?php
/**
 * $Author: BEESSHOP $
 * ============================================================================
 * ============================================================================
 */
namespace app\module\controllers;

use app\models\CateRecorder;
use app\models\GoodsRecorder;

class GoodslistController extends BaseController{

    //获取分类下内容
    //参数：$id-分类id,$page-分页,$sl_pr-排序,$sl_sr-排序
    public function actionList(){
        $post = \Yii::$app->request->post('page');
        $id = intval($post['id']);
        $page = intval($post['page'])-1;
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $sl_pr = intval($post['sl_pr']);
        $sl_sr = intval($post['sl_sr']);
        //判断是否顶级分类
        $cate = CateRecorder::find()->where('id=:id',[':id' => $id])->andWhere('parent_id=0')->one();
        $arr[]=$id;
        if($cate){
            //获取下级
            $cate_arr = CateRecorder::find()->select(['id'])->where('parent_id='.$cate->id)->asArray()->all();
            foreach($cate_arr as $v){
                $arr[]=$v['id'];
            }
        }
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num"])
        ->where('is_down=0')->andWhere(['in','cate_id',$arr]);
        switch($sl_pr){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('addtime DESC');
                break;
            case 2:
                $list = $list->orderBy('sell_num DESC');
                break;
        }
        switch ($sl_sr){
            case 0:
                break;
            case 1:
                $list=$list->orderBy('goods_price DESC');
                break;
            case 2:
                $list = $list->orderBy('goods_price ASC');
                break;
        }
        $list = $list->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_small_name'] = empty($v['goods_small_name'])?'':$v['goods_small_name'];
            $list[$k]['percent'] = $this->get_percent($v['goods_num'],$v['sell_num']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        if(count($list)<=0){
            $result = ['result'=>2,'data'=>'没有内容了'];
        }else{
            $result = ['result'=>1,'data'=>$list];
        }

        return json_encode($result);
    }


    //获取活动列表页内容
    //参数：$page-分页，$hd_type-活动类型，$id-活动id,$sl_pr-排序，$sl_sr-排序
    public function actionHdlist(){
        $post = \Yii::$app->request->post('page');
        $hd_type = $post['hd_type'];
        $id = intval($post['id']);
        $page = intval($post['page'])-1;
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $sl_pr = intval($post['sl_pr']);
        $sl_sr = intval($post['sl_sr']);
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num"])
            ->where('is_down=0');
        switch($hd_type){
            case "amount":
                if($id){
                    $list = $list->andWhere("amount_id=:id",[':id' => $id]);
                }else{
                    $list = $list->andWhere("amount_id!=0");
                }
                break;
            case "new":
                if($id){
                    $list = $list->andWhere("new_id=:id",[':id' => $id]);
                }else{
                    $list = $list->andWhere("new_id!=0");
                }
                break;
            case "dis":
                if($id){
                    $list = $list->andWhere("discount_id=:id",[':id' => $id]);
                }else{
                    $list = $list->andWhere("discount_id!=0");
                }
                break;
            case "pur":
                if($id){
                    $list = $list->andWhere("purchase_id=:id",[':id' => $id]);
                }else{
                    $list = $list->andWhere("purchase_id!=0");
                }
                break;
            default:
                break;
        }
        switch($sl_pr){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('addtime DESC');
                break;
            case 2:
                $list = $list->orderBy('sell_num DESC');
                break;
        }
        switch ($sl_sr){
            case 0:
                break;
            case 1:
                $list=$list->orderBy('goods_price DESC');
                break;
            case 2:
                $list = $list->orderBy('goods_price ASC');
                break;
        }
        $list = $list->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_small_name'] = empty($v['goods_small_name'])?'':$v['goods_small_name'];
            $list[$k]['percent'] = $this->get_percent($v['goods_num'],$v['sell_num']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        if(count($list)<=0){
            $result = ['result'=>2,'data'=>'没有内容了'];
        }else{
            $result = ['result'=>1,'data'=>$list];
        }

        return json_encode($result);
    }


    //获取模板标识下内容
    //参数：$mb_id-分类id,$page-分页,$sl_pr-排序,$sl_sr-排序
    public function actionListmb(){
        $post = \Yii::$app->request->post('page');
        $mb_id = intval($post['mb_id']);
        $page = intval($post['page'])-1;
        $pagesize = \Yii::$app->params['pagesize'];
        $offset = $page*$pagesize;
        $sl_pr = intval($post['sl_pr']);
        $sl_sr = intval($post['sl_sr']);
        $list = GoodsRecorder::find()->select(['id','goods_name','thumb_pic','goods_num','goods_unit','goods_price','goods_hd_price',"goods_small_name","sell_num"])
            ->where('is_down=0')->andWhere('goods_type=:id',[':id' => $mb_id]);
        switch($sl_pr){
            case 0:
                break;
            case 1:
                $list = $list->orderBy('addtime DESC');
                break;
            case 2:
                $list = $list->orderBy('sell_num DESC');
                break;
        }
        switch ($sl_sr){
            case 0:
                break;
            case 1:
                $list=$list->orderBy('goods_price DESC');
                break;
            case 2:
                $list = $list->orderBy('goods_price ASC');
                break;
        }
        $list = $list->limit($pagesize)->offset($offset)->asArray()->all();
        foreach($list as $k=>$v){
            $list[$k]['goods_small_name'] = empty($v['goods_small_name'])?'':$v['goods_small_name'];
            $list[$k]['percent'] = $this->get_percent($v['goods_num'],$v['sell_num']);
            $list[$k]['thumb_pic'] = \Yii::$app->params['base_url'].$v['thumb_pic'];
        }
        if(count($list)<=0){
            $result = ['result'=>2,'data'=>'没有内容了'];
        }else{
            $result = ['result'=>1,'data'=>$list];
        }

        return json_encode($result);
    }

}